package com.yoga.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yoga.entity.*;
import com.yoga.service.YogaUserService;
import com.yoga.service.CoachService;
import com.yoga.service.UserService;
import com.yoga.service.CoachprivateService;
import com.yoga.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Configuration
@RestController
public class YogaUserController {
    @Autowired
    YogaUserService yogaUserService;
    @Autowired
    UserService userService;
    @Autowired
    CoachprivateService coachprivateService;
    @Autowired
    CoachService coachService;

    //充值+缴费记录
    @RequestMapping("recharge")
    public ResultVO recharge(@RequestBody YogaTransaction yogaTransaction) {
        String uid = yogaTransaction.getUid();
        String type = yogaTransaction.getTtype();
        BigDecimal money = yogaTransaction.getTmoney();
        yogaUserService.saveTransaction(uid, type, money);
        return ResultVO.success("成");
    }

    //课程扣费
    @RequestMapping("deduction")
    public ResultVO deduction(Integer oid, BigDecimal money, Integer uid) {
        yogaUserService.updataMoney(oid, money, uid, null);
        return ResultVO.success("成");
    }


    //申诉单
    @RequestMapping("saveappeal")
    public ResultVO saveAppeal(UserAppeal userAppeal) {

        yogaUserService.saveAppeal(userAppeal);
        return ResultVO.success("成");
    }

    //场馆的教练
    @RequestMapping("getCoachAll")
    public ResultVO getCoachAll(Integer vid) {
        List<YogaUser> all = yogaUserService.findAll(vid);
        return ResultVO.success("成", all);
    }

    //场馆评分
    @RequestMapping("positive")
    public ResultVO positive(@RequestBody YogaVenue yogaVenue) {
        yogaUserService.positive(yogaVenue);
        return ResultVO.success("成");
    }

    //修改密码
    @RequestMapping("updtaPwd")
    public ResultVO updtaPwd(YogaUser yogaUser) {
        System.out.println(yogaUser.getUid());
        System.out.println(yogaUser.getUpwd());
        yogaUserService.updataUser(yogaUser);
        return ResultVO.success("成");
    }

//   个人信息补充+实名认证
    @RequestMapping("PersonInfo")
    public ResultVO PersonInfo(@RequestBody YogaUser yogaUser) {
        boolean flag = yogaUserService.updataUser(yogaUser);
        YogaUser yogaUser1 = yogaUserService.findOne(yogaUser.getUid());
        if(flag) {
            return ResultVO.success("成",yogaUser1);
        }
        return  ResultVO.fail("错误");
    }

    //个人钱包
    @RequestMapping("personMoney")
    public ResultVO personMoney(Integer uid) {
        YogaMymoney yogaMymoney = yogaUserService.personMoney(uid);
        if(yogaMymoney != null) {
            return ResultVO.success("成",yogaMymoney);
        }
        return  ResultVO.fail("错误");
    }

    //查出所有教练
    @RequestMapping("CoachAndUser")
    public ResultVO CoachAndUser() {
        List<YogaCoach> list = yogaUserService.findAllCoach();
        return  ResultVO.success("",list);
    }


    @RequestMapping("makeALesson")
    public ResultVO makeALesson(Integer uid,Integer coachUid,Integer cpid,@RequestBody List<String> requesttime){
        YogaMymoney yogaMymoney = userService.checkMyMoney(uid);
        Double userMoney =(yogaMymoney.getMoney()).doubleValue();
        YogaCoachprivate coachprivate = coachprivateService.getBycpid(cpid);
        Integer price = coachprivate.getPrice();
        System.out.println(requesttime.toString());
        if(userMoney<price){
            return ResultVO.fail("约课失败！账户余额不足！");
        }
        coachService.newLessonRequest(uid,coachUid,price,cpid,requesttime);
        return ResultVO.success("约课成功！等待教练确认！");
    }

    //教练的订单表
    @RequestMapping("coachByOrder")
    public ResultVO coachByPrivate(Integer coachuid, Integer PageSize, Integer PageNum) {
        PageHelper.startPage(PageNum, PageSize);
        List<YogaMyorder> list = yogaUserService.coachByPrivate(coachuid);
        PageInfo<YogaMyorder> pageInfo = new PageInfo<>(list);
        return ResultVO.success("查询成功", pageInfo);
    }

    //用户的订单表
    @RequestMapping("userByOrder")
    public ResultVO userByPrivate(String uid, Integer PageSize, Integer PageNum) {
        PageHelper.startPage(PageNum, PageSize);
        List<YogaMyorder> list = yogaUserService.userByPrivate(uid);
        PageInfo<YogaMyorder> pageInfo = new PageInfo<>(list);
        return ResultVO.success("查询成功", pageInfo);
    }

    @RequestMapping("checkMyMoney")
    public ResultVO checkMyMoney(Integer uid){
        YogaMymoney yogaMymoney = userService.checkMyMoney(uid);
        BigDecimal money = yogaMymoney.getMoney();
        return ResultVO.success("查询余额成功",money);
    }
}
